﻿#region File Description
//-----------------------------------------------------------------------------
// Program.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors; 
#endregion

namespace CompileEffect
{
    /// <summary>
    /// Commandline utility compiles .fx effect sources file into binary files that can be consumed by XNA Game Studio. 
    /// </summary>
    public class EffectReader
    {
        static public byte[] GetEffect(string sourceCode, GraphicsProfile targetProfile)
        {

            TargetPlatform targetPlatform = TargetPlatform.Windows; 
            ContentBuildLogger logger = new CustomLogger();

            // Import the effect source code.
            EffectImporter importer = new EffectImporter();
            ContentImporterContext importerContext = new CustomImporterContext(logger);
            EffectContent sourceEffect = importer.Import(sourceCode, importerContext);

            // Compile the effect.
            EffectProcessor processor = new EffectProcessor();
            ContentProcessorContext processorContext = new CustomProcessorContext(targetPlatform, targetProfile, logger);
            CompiledEffectContent compiledEffect = processor.Process(sourceEffect, processorContext);

            // Write out the compiled effect code.
            return compiledEffect.GetEffectCode();
        }
    }
}
